class OverNode extends CoolNode
{
	public constructor() 
	{
		super();
		this.init();
	}
	private m_againBtn:CoolBtn;
	private m_nextBtn:CoolBtn;
	private m_againCallback:Function;
	private m_nextCallback:Function;
	private m_timelabel:egret.TextField;
	private m_maxCommodity:CommodityBase;
	private m_CallbackObj:egret.DisplayObjectContainer;
	//private m_ScoreArray:Array<ScoreBase>;
	private m_NumLabelArray:Array<egret.TextField>;
	private init()
	{
		this.m_NumLabelArray = new Array<egret.TextField>();
		this.initBg();
		this.initBtn();
		this.initLabel();
		this.initNumberLabel();
	}
	public setCallback(again:Function, next:Function, obj:egret.DisplayObjectContainer)
	{
		this.m_againCallback = again;
		this.m_nextCallback = next;
		this.m_CallbackObj = obj;
	}
	public setEndInfo(scoreArray:Array<ScoreBase>)
	{
		console.log("AllPiece:" + ContentPieceFactory.getInstance().GetContentPieceLength());
		for(let i = 0; i < scoreArray.length; i++)
		{
			let tag:number = parseInt(scoreArray[i].id) - 1;
			this.m_NumLabelArray[tag].text = "x" + scoreArray[i].hitCount.toString();
		}
		this.m_maxCommodity = this.getMaxCommoditInfo(scoreArray);
		if(this.m_maxCommodity == null)
		{
			this.Failed();
		}
		else
		{
			this.Success();
		}
	}
	private Failed()
	{
		SoundEngine.getInstance().stopAllEffects();
		SoundEngine.getInstance().playEffect("win_mp3",1);
		this.m_nextBtn.visible = false;
		this.initFailedTitle();
		this.m_timelabel.text = "很抱歉，没有可领取物品";
		this.m_timelabel.anchorOffsetX = this.m_timelabel.width / 2;
		this.m_timelabel.anchorOffsetY = this.m_timelabel.height / 2;
		this.m_timelabel.x = this.width / 2;
		this.m_timelabel.y = this.height - this.m_againBtn.height * 2 - this.m_timelabel.height / 2;
	}
	private Success()
	{
		SoundEngine.getInstance().stopAllEffects();
		SoundEngine.getInstance().playEffect("win_mp3",1);
		this.m_againBtn.visible = false;
		this.m_timelabel.text = "可兑换物品:\n" + this.m_maxCommodity.name;
		this.m_timelabel.anchorOffsetX = this.m_timelabel.width / 2;
		this.m_timelabel.anchorOffsetY = this.m_timelabel.height / 2;
		this.m_timelabel.x = this.width / 2;
		this.m_timelabel.y = this.height - this.m_againBtn.height * 2 - this.m_timelabel.height / 2 + 10;
	}
	private getMaxCommoditInfo(scoreArray:Array<ScoreBase>):CommodityBase
	{
		if(scoreArray.length == 0)
		{
			return null;
		}
		let scoreInfo:ScoreBase = scoreArray[0];
		for(let i = 1; i < scoreArray.length; i++)
		{
			if(scoreInfo.hitCount < scoreArray[i].hitCount)
			{
				scoreInfo = scoreArray[i];
			}
		}
		if(scoreInfo.hitCount < DataManager.getInstance().m_Config_json.levelData.minCount)
		{
			return null;
		}
		let commodity:CommodityBase = DataManager.getInstance().getCommodityInfoById(scoreInfo.id);
		commodity.hitCount = scoreInfo.hitCount;
		return commodity;
	}
	private initNumberLabel()
	{
		for(let i = 0; i < 7; i++)
		{
			let h = Math.floor(i / 3);
			let l = i % 3;
			let numlabel = new egret.TextField();
			numlabel.size = 30;
			//numlabel.font = RES.getRes("youyuan_fnt");
			numlabel.verticalAlign = egret.VerticalAlign.MIDDLE;
			numlabel.text = "x0";
			numlabel.strokeColor = 0x000000;
        	numlabel.stroke = 2;
			numlabel.anchorOffsetX = numlabel.width / 2;
			numlabel.anchorOffsetY = numlabel.height / 2;
			numlabel.x = this.width / 2 - 120 + 120 * l;
			numlabel.y = 155 + h * 145;
			this.addChild(numlabel);
			this.m_NumLabelArray.push(numlabel);
		}
	}
	private initBg()
	{
		let bg = this.createBitmapByName("black");
		bg.alpha = 0.7;
		this.addChild(bg);
		bg.touchEnabled = true;//截取事件
		let board = this.createBitmapByName("board");
		
		board.anchorOffsetX = board.width / 2;
		board.anchorOffsetY = board.height / 2;
		board.x = this.width / 2;
		board.y = this.height / 2;
		this.addChild(board);
	}
	private initFailedTitle()
	{
		let title = this.createBitmapByName("failed");
		title.anchorOffsetX = title.width / 2;
		title.anchorOffsetY = title.height / 2;
		title.x = this.width / 2;
		title.y = this.height / 2 - title.height / 2;
		this.addChild(title);
	}
	private initLabel()
	{
		this.m_timelabel = new egret.TextField();
		this.m_timelabel.size = 25;
		this.m_timelabel.lineSpacing = 10;
		//this.m_timelabel.font = RES.getRes("youyuan_fnt");
		this.m_timelabel.textAlign = egret.HorizontalAlign.CENTER;
		this.m_timelabel.verticalAlign = egret.VerticalAlign.MIDDLE;
		this.m_timelabel.text = "很抱歉，没有可领取物品";
		this.m_timelabel.strokeColor = 0x000000;
        this.m_timelabel.stroke = 2;
		this.m_timelabel.anchorOffsetX = this.m_timelabel.width / 2;
		this.m_timelabel.anchorOffsetY = this.m_timelabel.height / 2;
		this.m_timelabel.x = this.width / 2;
		this.m_timelabel.y = this.height - this.m_againBtn.height * 2 - this.m_timelabel.height / 2;
		this.addChild(this.m_timelabel);
	}
	private initBtn()
	{
		let againBtn = this.createBtnByName("again",this.clickAgain,this);
		let nextBtn = this.createBtnByName("duihuan",this.clickNext,this);
		againBtn.x = this.width / 2;
		againBtn.y = this.height - againBtn.height * 1.2;
		nextBtn.x = this.width / 2;
		nextBtn.y = this.height - againBtn.height * 1.2;
		this.addChild(againBtn);
		this.addChild(nextBtn);
		this.m_againBtn = againBtn;
		this.m_nextBtn = nextBtn;
	}
	private clickAgain(event:egret.Event)
	{
		let callback = this.m_againCallback.bind(this.m_CallbackObj);
		callback();
	}
	private clickNext(event:egret.Event)
	{
		class HttpManagerDataCallback implements HttpManagerCallback
        {      
            getDataSucceed()
            {
				console.log(DataManager.getInstance().m_httpData);
                window.location.href = DataManager.getInstance().m_httpData.url;
            }
            getDataFailed()
            {
                alert("游戏数据出错"); 
            }
            getDataProgress(current:number,total:number)
            {
                
            }
        }
        let httpcallback = new HttpManagerDataCallback();

		let upc = this.m_maxCommodity.upc;
		let hitCount = this.m_maxCommodity.hitCount;
		if(hitCount > DataManager.getInstance().m_Config_json.levelData.maxCount)
		{
			hitCount = DataManager.getInstance().m_Config_json.levelData.maxCount;
		}
		let url = DataManager.getInstance().m_Config_json.url + "/" + upc + "/" + hitCount;
		//let url = "http://175.102.18.87/api/gamedata/A002";
		console.log("url:" + url);
		HttpManager.getInstance().init(url);
		HttpManager.getInstance().getAllData(httpcallback);
		let callback = this.m_nextCallback.bind(this.m_CallbackObj);
		callback();
	}
}